package com.sinaapp.mrblog.game.Assasin.stage;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.sinaapp.mrblog.game.Assasin.model.ACircle;
import com.sinaapp.mrblog.game.Assasin.model.Alert;
import com.sinaapp.mrblog.game.Assasin.model.Bloodstain;
import com.sinaapp.mrblog.game.Assasin.model.Boss;
import com.sinaapp.mrblog.game.Assasin.model.Bullet;
import com.sinaapp.mrblog.game.Assasin.model.BulletDust;
import com.sinaapp.mrblog.game.Assasin.model.Footprint;
import com.sinaapp.mrblog.game.Assasin.model.Hole;
import com.sinaapp.mrblog.game.Assasin.model.Killer;
import com.sinaapp.mrblog.game.Assasin.model.Policeman;
import com.sinaapp.mrblog.game.Assasin.stage.GameStage.GameStageListener;
import com.sinaapp.mrblog.game.Assasin.util.AssasinManager;

public class GSListener implements GameStageListener{
	private boolean alertByBossEnable = true;
	private GameStage gameStage;
	
	public GSListener(GameStage gameStage){
		this.gameStage = gameStage;
	}
	
	@Override
	public void unalert() {
		gameStage.state = GameStage.STAGE_RUNNING;
		AssasinManager.alertMusic.pause();
		for(Policeman p:GameStage.policemans){
			p.setState(Policeman.POLICEMAN_STATE_PATROL);
		}
		if(GameStage.hole.getState()==Hole.HOLE_STATE_LOCK){
			GameStage.hole.setState(Hole.HOLE_STATE_ENABLE);
		}
		gameStage.alert.unalert();
	}
	
	@Override
	public void shoot(Bullet bullet) {
		int index = MathUtils.random(AssasinManager.shootSound.length-1);
		AssasinManager.playSound(AssasinManager.shootSound[index]);	
		GameStage.bullets.add(bullet);
		gameStage.addActor(bullet);
	}
	
	@Override
	public void killKiller() {
		AssasinManager.playSound(AssasinManager.killerDeadSound);
		GameStage.killer.setState(Killer.KILLER_STATE_DEAD);
		Bloodstain bs = new Bloodstain(GameStage.killer, Bloodstain.BLOODSTAIN_STATE_DEAD);
		GameStage.bloodstains.add(bs);
		unalert();
		gameStage.addActor(bs);
	}
	
	@Override
	public void killBoss() {
		if(GameStage.boss.getState()!=Boss.BOSS_STATE_DEAD){
			AssasinManager.playSound(AssasinManager.bossDeadSound);
			GameStage.boss.setState(Boss.BOSS_STATE_DEAD);
			gameStage.addActor(new Bloodstain(GameStage.boss, Bloodstain.BLOODSTAIN_STATE_DEAD));
			Bloodstain bs = new Bloodstain(GameStage.killer, Bloodstain.BLOODSTAIN_STATE_SHOOT);
			GameStage.bloodstains.add(bs);
			gameStage.addActor(bs);
			if(gameStage.state==GameStage.STAGE_ALERT){
				GameStage.hole.setState(Hole.HOLE_STATE_LOCK);
			}else{
				GameStage.hole.setState(Hole.HOLE_STATE_ENABLE);
			}
			
//			GameStage.killer.touchFloodTime = Footprint.TOUCH_FLOOD_TIME;
		}
	}
	
	@Override
	public void alert() {
		gameStage.setState(GameStage.STAGE_ALERT);
		GameStage.alert.alert();
		AssasinManager.playMusic(AssasinManager.alertMusic);
		for(Policeman p:GameStage.policemans){
			p.setState(Policeman.POLICEMAN_STATE_ALERT_PATROL);
		}
		if(GameStage.hole.getState()==Hole.HOLE_STATE_ENABLE){
			GameStage.hole.setState(Hole.HOLE_STATE_LOCK);
		}
		
	}

	@Override
	public void shooted(Bullet bullet) {
		GameStage.killer.shootted(bullet);
		Bloodstain bs = new Bloodstain(GameStage.killer, Bloodstain.BLOODSTAIN_STATE_SHOOT);
		GameStage.bloodstains.add(bs);
		gameStage.addActor(bs);
	}

	

	@Override
	public void footprint(Footprint footprint) {
		gameStage.addActor(footprint);
		GameStage.footprints.add(footprint);
	}
	
	@Override
	public void bulletDust(BulletDust bd) {
		GameStage.bulletDusts.add(bd);
		gameStage.addActor(bd);
	}
	
	
	@Override
	public void clear() {
		if(GameStage.bullets!=null){
			for(int i = 0; i<GameStage.bullets.size();i++){
				Bullet bullet = GameStage.bullets.get(i); 
				if(bullet.getState() == Bullet.BULLET_STATE_DIED){
					GameStage.bullets.remove(i);
					bullet.remove();
					i--;
				}
			}
			
		}
		
		if(GameStage.footprints!=null){
			for(int i = 0;i<GameStage.footprints.size();i++){
				Footprint fp = GameStage.footprints.get(i);
				if(fp.getState()==Footprint.FOOTPRINT_STATE_DIED){
					GameStage.footprints.remove(i);
					fp.remove();
					i--;
				}
			}
		}
		
		if(GameStage.bulletDusts!=null){
			for(int i = 0;i<GameStage.bulletDusts.size();i++){
				BulletDust bd = GameStage.bulletDusts.get(i);
				if(bd.getState()==Footprint.FOOTPRINT_STATE_DIED){
					GameStage.bulletDusts.remove(i);
					bd.remove();
					i--;
				}
			}
		}
		
		if(GameStage.circles!=null){
			for(int i=0;i<GameStage.circles.size();i++){
				ACircle circle = GameStage.circles.get(i);
				if(circle.getState()==ACircle.STATE_DEAD){
					GameStage.circles.remove(i);
					circle.remove();
					i--;
				}
			}
		}
		
	}

	@Override
	public void alertByBossDead() {
		if(alertByBossEnable){
			alert();
			alertByBossEnable = false;
		}
	}

	
}
